/**
 * Copyright [2012-2013] [Guðmundur Björn Birkisson]
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. Copyright [2012-2013] [Guðmundur Björn Birkisson]
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * ---------------------------------------------------------------------------
 * 
 * Class name:  FunDecl
 * 
 * Description of class:
 * 
 * Concrete implementation of LangObject
 * 
 */

package karel.lang.secondpass;

import java.io.IOException;
import karel.lang.RDParser;
import karel.lang.newParserTools.RDGoCounter;


public class FunDecl extends Program {
    
    public String name;
    private int nrOfParams;
    public int pos;
    
    public FunDecl (String name, int line, int column) {
        super(line,column);
        this.name = name;
        
    }
    
    public void setNrOfParams(int nrOfParams) {
        this.nrOfParams = nrOfParams;
        this.currentPos = nrOfParams;
    }
    
    public int getNrOfParams() {
        return nrOfParams;
    }
    
    @Override
    public void add(LangObject obj) throws IOException {
        try {
            super.add(obj);
        } catch(IOException e) {
            if(!(obj instanceof Return)) {
                throw e;
            } else {
                objs.add(obj);
            }
        }
    }
    
    @Override
    public void generate(RDParser parser) throws java.io.IOException {
        int go0 = RDGoCounter.getInstance().push();
        int go1 = RDGoCounter.getInstance().push();
        parser.asmCode.append("(MakeClosure _").append(go0).append(" 0 ").append(nrOfParams).append(")\n");
        parser.asmCode.append("(Go _").append(go1).append(")\n");
        parser.asmCode.append("_").append(go0).append(":\n");
        super.generate(parser);
        parser.asmCode.append("(MakeVal null)\n");
        parser.asmCode.append("(Return 0)\n");
        parser.asmCode.append("_").append(go1).append(":\n");
        parser.asmCode.append("(Store 0 ").append(pos).append(")\n");
    }
}
